import time
from pprint import pprint

from lxml import etree
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import time

# 打开浏览器
BROWSER = webdriver.Chrome()
BROWSER.set_window_size(1600, 4000)
# 隐式等待
WAIT = WebDriverWait(BROWSER, 5)


def get_page(url, page):
    """获取网页"""

    if page == 1:
        BROWSER.get(url)
        # 一直等到页面元素可以定位为止，但是要在WAIT时间之内
        input_el = WAIT.until(ec.presence_of_element_located((By.CSS_SELECTOR, '#key')))
        # 清除输入框的内容
        input_el.clear()
        input_el.send_keys('高达')
        input_el.send_keys('\n')  # 回车确定

    # 点击对象要用element_to_be_clickable(如果没有回车确定则需要用点击)
    # search_button = WAIT.until(ec.element_to_be_clickable((By.CSS_SELECTOR, 'div#search button.button')))
    # search_button.click()

    # 等待后返回页面的内容
    time.sleep(3)

    # 滚动太快拿不到图片，则需要慢慢滚
    for i in range(1, 9):

        # 模拟鼠标滚动，让图片加载，执行原生js代码
        str_js1 = 'var scrollHeight = document.body.scrollHeight;'  # 拿到整个页面的滚动最大高度
        str_js2 = f'window.scrollTo(0, {i} * (scrollHeight/8))'  # 第一个参数是横向滚动到的坐标点，第二个是纵向滚动到的坐标点
        BROWSER.execute_script(str_js1 + str_js2)

        time.sleep(0.5)

    # 快速滚到底
    # BROWSER.execute_script('var scrollHeight = document.body.scrollHeight;window.scrollTo(0, scrollHeight)')
    # time.sleep(2)

    html = BROWSER.page_source

    # 输入翻页(如果输入框不在屏幕中会出问题，所以我将浏览器高度调为4000，如果不够则需要滚动到该节点位置)
    page_input = WAIT.until(ec.presence_of_element_located((By.CSS_SELECTOR, '#J_bottomPage input.input-txt')))
    # 清除输入框的内容
    page_input.clear()
    page_input.send_keys(f'{page}\n')
    
    time.sleep(0.5)

    return html


def parse_page(html):
    """解析网页"""

    etree_html = etree.HTML(html)
    li_list = etree_html.xpath('//div[@id="J_goodsList"]/ul/li')
    titles = []
    images = []
    for li in li_list:
        # 获取标题
        title = li.xpath('string(./div/div[@class="p-name p-name-type-2"]/a/em)')
        titles.append(title)
        # 获取图片
        image = li.xpath('.//img/@src')
        image = ('http:' + image[0]) if image else '没有取到'
        images.append(image)

    pprint(titles)
    pprint(images)
    print(len(titles))
    print(len(images))


def main():
    """主函数"""

    url = 'https://www.jd.com/'

    for page in range(1, 101):
        print(page)
        html = get_page(url, page)
        parse_page(html)


if __name__ == '__main__':
    main()
